<?php

declare (strict_types=1);

namespace app\api\middleware;

use app\enum\Key;
use app\enum\StatusCode;
use app\exception\BusinessException;
use think\facade\Session;

class Check
{
    /** 不判断登录的uri
     * @var string[]
     */
    protected $whiteListUrl = [
        '/api/user/login',
        '/api/user/register',
    ];

    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return \think\Response|\think\response\Redirect
     */
    public function handle($request, \Closure $next)
    {

        if (in_array($request->baseUrl(), $this->whiteListUrl)) {
            return $next($request);
        }
        if (!Session::get(Key::LOGIN_USER->value)) {
            throw new BusinessException(StatusCode::NO_LOGIN->value);
        }

        return $next($request);
    }
}
